//
//  ESDescribeView.m
//  bus
//
//  Created by hychen on 2017/4/12.
//  Copyright © 2017年 Hychen. All rights reserved.
//

#import "ESDescribeView.h"

static CGFloat margin  = 10.f;
static CGFloat padding = 15.f;

#define kScreenH       [UIScreen mainScreen].bounds.size.height // 屏幕高度
#define kScreenW       [UIScreen mainScreen].bounds.size.width  // 屏幕宽度

@interface ESDescribeView ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *pageLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@end

@implementation ESDescribeView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _titleFontSize = 16;
        _contentFontSize = 12;
        [self prepareUI];
    }
    return self;
}

- (void)prepareUI{
    
    self.showsVerticalScrollIndicator = false;
    self.showsHorizontalScrollIndicator = false;
    self.pagingEnabled = NO;
    
    self.backgroundColor = [UIColor colorWithRed:(41.f)/255.f green:(41.f)/255.f blue:(41.f)/255.f alpha:(0.6f)];
    [self addSubview:self.titleLabel];
    [self addSubview:self.pageLabel];
    [self addSubview:self.contentLabel];
}

// 父类调用setNeedsLayout刷新布局
- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGSize pageSize = [self sizeWithString:[NSString stringWithFormat:@"%ld/%ld",self.currentIndex,self.allIndex] font:[UIFont systemFontOfSize:13] constraintSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    
    CGFloat titleW = kScreenW - margin*2 - padding - pageSize.width - 7;
    
    CGSize titleSize = [self sizeWithString:self.title font:[UIFont systemFontOfSize:self.titleFontSize] constraintSize:CGSizeMake(titleW, MAXFLOAT)];
    CGSize contentSize = [self sizeWithString:self.content font:[UIFont systemFontOfSize:self.contentFontSize] constraintSize:CGSizeMake(kScreenW-margin*2, MAXFLOAT)];
    
    CGFloat titleH = titleSize.height;
    CGFloat pageW = pageSize.width;
    CGFloat pageH = 18;
    CGFloat pageX = kScreenW - margin - pageW;
    CGFloat contentY = margin*2+titleH;
    CGFloat contentW = kScreenW - margin*2;
    CGFloat contentH = contentSize.height;
    self.pageLabel.frame = CGRectMake(pageX-2, margin, pageW+5, pageH);
    self.titleLabel.frame = CGRectMake(margin, margin,  titleW, titleH);
    self.contentLabel.frame = CGRectMake(margin, contentY, contentW, contentH);
    self.contentSize = CGSizeMake(self.frame.size.width, CGRectGetMaxY(self.contentLabel.frame)+10);
}



#pragma mark - private method
- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font constraintSize:(CGSize)constraintSize
{
    CGSize stringSize = CGSizeZero;
    
    NSDictionary *attributes = @{NSFontAttributeName:font};
    NSInteger options = NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin;
    CGRect stringRect = [string boundingRectWithSize:constraintSize options:options attributes:attributes context:NULL];
    stringSize = stringRect.size;
    
    return stringSize;
}

#pragma mark - setter
- (void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}

- (void)setContent:(NSString *)content{
    _content = content;
    self.contentLabel.text = content;
}

- (void)setAllIndex:(NSInteger)allIndex{
    _allIndex = allIndex;
    self.pageLabel.text = [NSString stringWithFormat:@"%ld/%ld",self.currentIndex+1,allIndex];
}

- (void)setCurrentIndex:(NSInteger)currentIndex{
    _currentIndex = currentIndex;
    self.pageLabel.text = [NSString stringWithFormat:@"%ld/%ld",currentIndex+1,self.allIndex];
}

#pragma mark - getter
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:self.titleFontSize];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

- (UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.font = [UIFont systemFontOfSize:self.contentFontSize];
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.textColor = [UIColor colorWithRed:(240.0 / 255.0) green:(240.0 / 255.0) blue:(240.0 / 255.0) alpha:1.0];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

- (UILabel *)pageLabel{
    if (!_pageLabel) {
        _pageLabel = [[UILabel alloc] init];
        _pageLabel.font = [UIFont systemFontOfSize:15];
        _pageLabel.textAlignment = NSTextAlignmentRight;
        _pageLabel.textColor = [UIColor whiteColor];
        _pageLabel.numberOfLines = 1;
    }
    return _pageLabel;
}

@end
